package com.hjq.fcloud.presenter

import android.text.TextUtils
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.http.api.GetSelectCodeApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback

/**
 * @author : fenghailin
 * Createtime: 2023/1/19
 * 包名：com.hjq.fcloud.presenter
 * 说明：type为select类型的公共逻辑处理
 */
object SelectPresenter {

    fun getSelect(
        ac: AppActivity,
        selectCode: String,
        callBack: (selectDatas: List<GetSelectCodeApi.DataDTO>) -> Unit
    ) {
        EasyHttp.post(ac).api(
                GetSelectCodeApi().setSelectCode(selectCode)
            ).request(object : HttpCallback<HttpData<ArrayList<GetSelectCodeApi.DataDTO>?>>(ac) {
                override fun onSucceed(data: HttpData<ArrayList<GetSelectCodeApi.DataDTO>?>) {
                    if (data != null) {
                        callBack(data.getData()!!)
                    }
                }
            })
    }


    fun getNameById(mSelectDatas: List<GetSelectCodeApi.DataDTO>, defaultLable: String): String {
        mSelectDatas.forEach {
            if (it.label == defaultLable) {
                return it.value.toString()
            }
        }
        return ""
    }


    fun convertToList(mSelectDatas: List<GetSelectCodeApi.DataDTO>?): MutableList<String> {
        val datas: MutableList<String> = java.util.ArrayList()
        if (mSelectDatas != null && mSelectDatas!!.size > 0) {
            mSelectDatas?.filter {
                !TextUtils.isEmpty(it.label)
            }?.forEach {
                datas.add(it.label)
            }
        }
        return datas
    }
}